/*
 * @(#)InterestPrincipal.java    Dec 22, 2010
 *
 * Copyright 2009 Nyber Shanghai, Inc. All rights reserved.
 */

package com.lab.puzzle;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Class description goes here.
 *
 * @author nickevin@gmail.com
 * @version Dec 22, 2010 8:26:50 PM
 */

public class PolicyChange {

    /**
     * 计算保全补缴保费利息.
     * 
     * @param periodPrem
     *            期缴保费
     * @param startDate
     *            利息起始日期
     * @param endDate
     *            利息终止日期
     * @param premEndDate
     *            保费终止日期
     * @param chargeMode
     *            缴费方式
     * @return
     */
    private BigDecimal calcPremInterest(BigDecimal periodPrem, Date startDate,
            Date endDate, Date premEndDate, String chargeMode) {
        int monthMode = 1;
        if (isMonthCharge(chargeMode)) {
            monthMode = 1;
        } else if (isYearCharge(chargeMode)) {
            monthMode = 12;
        }

        BigDecimal interest = calcCompoundInterest(periodPrem, startDate,
                endDate, premEndDate, monthMode);

        return null;
    }

    private BigDecimal calcCompoundInterest(BigDecimal periodPrem,
            Date startDate, Date endDate, Date premEndDate, int monthMode) {
        BigDecimal lastestRate = new BigDecimal("0.052500");
        BigDecimal interest = new BigDecimal("0.000000");

        while (startDate.before(premEndDate)) {
            interest.add(calcPeriodInterest(periodPrem, startDate, endDate,
                    lastestRate));
        }

        return null;
    }

    /**
     * 计算从某个缴费期开始, 到某个时间点为止一期保费应补利息.
     * 
     * @param periodPrem
     * @param startDate
     * @param endDate
     * @param lastestRate
     * @return
     */
    private BigDecimal calcPeriodInterest(BigDecimal periodPrem,
            Date startDate, Date endDate, BigDecimal lastestRate) {
        BigDecimal principal = new BigDecimal("0.000000");
        BigDecimal interest = new BigDecimal("0.000000");

        while (startDate.before(endDate)) {
            BigDecimal halfAYearOfDays = plusMonths(endDate, 6);
            BigDecimal wholeYearOfDays = plusMonths(endDate, 6);

            interest = principal.multiply(lastestRate).multiply(
                    halfAYearOfDays.divide(wholeYearOfDays));

            principal = principal.add(interest);

        }

        return null;
    }

    private BigDecimal plusMonths(Date endDate, int i) {

        return null;
    }

    /**
     * 月缴.
     * 
     * @param chargeMode
     * @return
     */
    private boolean isMonthCharge(String chargeMode) {
        return chargeMode.equalsIgnoreCase("4");
    };

    private boolean isYearCharge(String chargeMode) {
        return chargeMode.equalsIgnoreCase("1");
    }
}
